home *** CD-ROM | disk | FTP | other *** search
/ QRZ! Ham Radio 8 / QRZ Ham Radio Callsign Database - Volume 8.iso / pc / files / t_unix / j109lxa4.tar / ppp.c < prev    next >
C/C++ Source or Header  |  1994-06-04  |  21KB  |  832 lines

  1. /*
  2.  *  PPP.C    -- Send and receive datagrams on serial lines with
  3.  *           Point-to-Point Protocol
  4.  *
  5.  *    This implementation of PPP is declared to be in the public domain.
  6.  *
  7.  *    Based (in part) upon previous implementations by:
  8.  *    1989    -- Drew Daniel Perkins        (ddp@andrew.cmu.edu)
  9.  *           Carnegie Mellon University
  10.  *    09-90    -- Katie Stevens        (dkstevens@ucdavis.edu)
  11.  *           UC Davis, Computing Services
  12.  *
  13.  *    Jan 91    Bill_Simpson@um.cc.umich.edu
  14.  *        Computer Systems Consulting Services
  15.  *
  16.  *    Feb 91    Glenn McGregor            (ghm@merit.edu)
  17.  *        Testing and suggestions.
  18.  *
  19.  *    May 91    Bill Simpson & Glenn McGregor
  20.  *        Update to newest LCP and IPCP draft RFCs.
  21.  *        Add quick installation features.
  22.  *        Add support for echo and discard message sending.
  23.  *
  24.  *    Jul 91    Glenn McGregor & Bill Simpson
  25.  *        Improve PAP user interface and fix related bugs.
  26.  *        Remove pwaits and "phase machine".
  27.  */
  28.  
  29. #include "global.h"
  30. #include "mbuf.h"
  31. #include "proc.h"
  32. #include "iface.h"
  33. #include "internet.h"
  34. #include "ip.h"
  35. #include "slhc.h"
  36. #ifdef UNIX
  37. #include "unixasy.h"
  38. #else
  39. #include "i8250.h"
  40. #endif
  41. #include "asy.h"
  42. #include "pktdrvr.h"
  43. #include "socket.h"
  44. #include "devparam.h"
  45. #include "ppp.h"
  46. #include "pppfsm.h"
  47. #include "ppplcp.h"
  48. #include "ppppap.h"
  49. #include "pppipcp.h"
  50. #include "trace.h"
  51.  
  52. /* Routines local to this file */
  53. static struct mbuf *htonppp __ARGS((struct ppp_hdr *ppp, struct mbuf *data));
  54.  
  55. static void ppp_log __ARGS((struct ppp_s *ppp_p, char *comment));
  56. static void ppp_error __ARGS((struct ppp_s *ppp_p, struct mbuf *bp, char *comment));
  57. static void ppp_skipped __ARGS((struct ppp_s *ppp_p, struct mbuf *bp, char *comment));
  58.  
  59. static int ppp_raw __ARGS((struct iface *ifp, struct mbuf *data));
  60.  
  61. static void ppp_recv __ARGS((int dev, void *p1, void *p2));
  62.  
  63. static int ppp_iostatus __ARGS((struct iface *ifp, int command, int32 value));
  64. static int ppp_discard     __ARGS((struct iface *ifp, struct mbuf *bp));
  65. static int ppp_echo    __ARGS((struct iface *ifp, struct mbuf *bp));
  66.  
  67.  
  68. /*
  69.  * FCS lookup table as generated by fcsgen.c
  70.  */
  71. int16 fcstab[256] = {
  72.     0x0000, 0x1189, 0x2312, 0x329b, 0x4624, 0x57ad, 0x6536, 0x74bf,
  73.     0x8c48, 0x9dc1, 0xaf5a, 0xbed3, 0xca6c, 0xdbe5, 0xe97e, 0xf8f7,
  74.     0x1081, 0x0108, 0x3393, 0x221a, 0x56a5, 0x472c, 0x75b7, 0x643e,
  75.     0x9cc9, 0x8d40, 0xbfdb, 0xae52, 0xdaed, 0xcb64, 0xf9ff, 0xe876,
  76.     0x2102, 0x308b, 0x0210, 0x1399, 0x6726, 0x76af, 0x4434, 0x55bd,
  77.     0xad4a, 0xbcc3, 0x8e58, 0x9fd1, 0xeb6e, 0xfae7, 0xc87c, 0xd9f5,
  78.     0x3183, 0x200a, 0x1291, 0x0318, 0x77a7, 0x662e, 0x54b5, 0x453c,
  79.     0xbdcb, 0xac42, 0x9ed9, 0x8f50, 0xfbef, 0xea66, 0xd8fd, 0xc974,
  80.     0x4204, 0x538d, 0x6116, 0x709f, 0x0420, 0x15a9, 0x2732, 0x36bb,
  81.     0xce4c, 0xdfc5, 0xed5e, 0xfcd7, 0x8868, 0x99e1, 0xab7a, 0xbaf3,
  82.     0x5285, 0x430c, 0x7197, 0x601e, 0x14a1, 0x0528, 0x37b3, 0x263a,
  83.     0xdecd, 0xcf44, 0xfddf, 0xec56, 0x98e9, 0x8960, 0xbbfb, 0xaa72,
  84.     0x6306, 0x728f, 0x4014, 0x519d, 0x2522, 0x34ab, 0x0630, 0x17b9,
  85.     0xef4e, 0xfec7, 0xcc5c, 0xddd5, 0xa96a, 0xb8e3, 0x8a78, 0x9bf1,
  86.     0x7387, 0x620e, 0x5095, 0x411c, 0x35a3, 0x242a, 0x16b1, 0x0738,
  87.     0xffcf, 0xee46, 0xdcdd, 0xcd54, 0xb9eb, 0xa862, 0x9af9, 0x8b70,
  88.     0x8408, 0x9581, 0xa71a, 0xb693, 0xc22c, 0xd3a5, 0xe13e, 0xf0b7,
  89.     0x0840, 0x19c9, 0x2b52, 0x3adb, 0x4e64, 0x5fed, 0x6d76, 0x7cff,
  90.     0x9489, 0x8500, 0xb79b, 0xa612, 0xd2ad, 0xc324, 0xf1bf, 0xe036,
  91.     0x18c1, 0x0948, 0x3bd3, 0x2a5a, 0x5ee5, 0x4f6c, 0x7df7, 0x6c7e,
  92.     0xa50a, 0xb483, 0x8618, 0x9791, 0xe32e, 0xf2a7, 0xc03c, 0xd1b5,
  93.     0x2942, 0x38cb, 0x0a50, 0x1bd9, 0x6f66, 0x7eef, 0x4c74, 0x5dfd,
  94.     0xb58b, 0xa402, 0x9699, 0x8710, 0xf3af, 0xe226, 0xd0bd, 0xc134,
  95.     0x39c3, 0x284a, 0x1ad1, 0x0b58, 0x7fe7, 0x6e6e, 0x5cf5, 0x4d7c,
  96.     0xc60c, 0xd785, 0xe51e, 0xf497, 0x8028, 0x91a1, 0xa33a, 0xb2b3,
  97.     0x4a44, 0x5bcd, 0x6956, 0x78df, 0x0c60, 0x1de9, 0x2f72, 0x3efb,
  98.     0xd68d, 0xc704, 0xf59f, 0xe416, 0x90a9, 0x8120, 0xb3bb, 0xa232,
  99.     0x5ac5, 0x4b4c, 0x79d7, 0x685e, 0x1ce1, 0x0d68, 0x3ff3, 0x2e7a,
  100.     0xe70e, 0xf687, 0xc41c, 0xd595, 0xa12a, 0xb0a3, 0x8238, 0x93b1,
  101.     0x6b46, 0x7acf, 0x4854, 0x59dd, 0x2d62, 0x3ceb, 0x0e70, 0x1ff9,
  102.     0xf78f, 0xe606, 0xd49d, 0xc514, 0xb1ab, 0xa022, 0x92b9, 0x8330,
  103.     0x7bc7, 0x6a4e, 0x58d5, 0x495c, 0x3de3, 0x2c6a, 0x1ef1, 0x0f78
  104. };
  105.  
  106. #define pppfcs(fcs, c)        ((fcs >> 8) ^ fcstab[(fcs ^ c) & 0x00ff])
  107. #define SP_CHAR            0x20
  108.  
  109.  
  110. /****************************************************************************/
  111.  
  112. /* Convert PPP header in host form to network form */
  113. static struct mbuf *
  114. htonppp(ppp, data)
  115. struct ppp_hdr *ppp;
  116. struct mbuf *data;
  117. {
  118.     struct mbuf *bp;
  119.     register char *cp;
  120.  
  121.     /* Prepend header onto packet data */
  122.     if ((bp = pushdown(data, PPP_HDR_LEN)) == NULLBUF) {
  123.         free_p( data );
  124.         return NULLBUF;
  125.     }
  126.  
  127.     /* Load header with proper values */
  128.     cp = bp->data;
  129.     *cp++ = ppp->addr;
  130.     *cp++ = ppp->control;
  131.     cp = put16(cp,ppp->protocol);
  132.  
  133.     return bp;
  134. }
  135.  
  136.  
  137. /************************************************************************/
  138. /* General log routine */
  139. static void
  140. ppp_log( ppp_p, comment )
  141. struct ppp_s *ppp_p;
  142. char *comment;
  143. {
  144.     if (ppp_p->trace)
  145.         trace_log(ppp_p->iface,"%s PPP %s",
  146.             ppp_p->iface->name,
  147.             comment);
  148. }
  149.  
  150.  
  151. /* Bad packet */
  152. static void
  153. ppp_error(ppp_p, bp, comment)
  154. struct ppp_s *ppp_p;
  155. struct mbuf *bp;
  156. char *comment;
  157. {
  158.     free_p(bp);
  159.     ppp_log( ppp_p, comment );
  160. }
  161.  
  162.  
  163. /* Unknown type input packet */
  164. static void
  165. ppp_skipped(ppp_p, bp, comment)
  166. struct ppp_s *ppp_p;
  167. struct mbuf *bp;
  168. char *comment;
  169. {
  170.     struct ipcp_s *ipcp_p;
  171.  
  172.     if ( (ipcp_p = ppp_p->fsm[IPcp].pdv) != NULL ) {
  173.         slhc_toss( ipcp_p->slhcp );
  174.     }
  175.     ppp_error( ppp_p, bp, comment );
  176. }
  177.  
  178.  
  179. /****************************************************************************/
  180. /* Send IP datagram with Point-to-Point Protocol */
  181. int
  182. ppp_send(bp,ifp,gateway,prec,del,tput,rel)
  183. struct mbuf *bp;    /* Buffer to send */
  184. struct iface *ifp;    /* Pointer to interface control block */
  185. int32 gateway;        /* Ignored (PPP is point-to-point) */
  186. int prec;
  187. int del;
  188. int tput;
  189. int rel;
  190. {
  191.     struct ppp_s *ppp_p;
  192.     struct ipcp_s *ipcp_p;
  193.     int protocol = PPP_IP_PROTOCOL;
  194.  
  195.     if (ifp == NULLIF
  196.      || (ppp_p = ifp->edv) == NULL) {
  197.         free_p(bp);
  198.         return -1;
  199.     }
  200.  
  201.     if (ppp_p->fsm[IPcp].state != fsmOPENED) {
  202.         ppp_error( ppp_p, bp, "not open for IP traffic" );
  203.         ppp_p->OutError++;
  204.         return -1;
  205.     }
  206.  
  207.     ipcp_p = ppp_p->fsm[IPcp].pdv;
  208.     if (ipcp_p->remote.work.negotiate & IPCP_N_COMPRESS) {
  209.         /* Attempt IP/TCP header compression */
  210.         switch ( slhc_compress(ipcp_p->slhcp, &bp,
  211.             ipcp_p->remote.work.slot_compress) ) {
  212.         case SL_TYPE_IP:
  213.             protocol = PPP_IP_PROTOCOL;
  214.             break;
  215.         case SL_TYPE_COMPRESSED_TCP:
  216.             protocol = PPP_COMPR_PROTOCOL;
  217.             break;
  218.         case SL_TYPE_UNCOMPRESSED_TCP:
  219.             protocol = PPP_UNCOMP_PROTOCOL;
  220.             break;
  221.         default:
  222.             ppp_error( ppp_p, bp, "bad IP packet" );
  223.             ppp_p->OutError++;
  224.             return -1;
  225.         };
  226.     }
  227.     return (*ifp->output)(ifp,NULLCHAR,NULLCHAR,protocol,bp);
  228. }
  229.  
  230.  
  231. /* Send a packet with PPP header */
  232. int
  233. ppp_output(ifp,dest,source,protocol,data)
  234. struct iface *ifp;    /* Pointer to interface control block */
  235. char *dest;        /* Dest addr (ignored; PPP is point-to-point) */
  236. char *source;        /* Source addr (ignored; PPP is point-to-point) */
  237. int16 protocol;        /* PPP Protocol Type field */
  238. struct mbuf *data;    /* Actual data to be sent */
  239. {
  240.     struct ppp_s *ppp_p;
  241.     struct mbuf *bp;
  242.     struct ppp_hdr hdr;
  243.  
  244.     if (ifp == NULLIF
  245.      || (ppp_p = ifp->edv) == NULL) {
  246.         free_p(data);
  247.         return -1;
  248.     }
  249.  
  250.     if (ppp_p->phase == pppDEAD) {
  251.         ppp_error( ppp_p, data, "line not up" );
  252.         ppp_p->OutError++;
  253.         return -1;
  254.     }
  255.  
  256.     hdr.addr = HDLC_ALL_ADDR;
  257.     hdr.control = HDLC_UI;
  258.     hdr.protocol = protocol;
  259.  
  260.     if ((bp = htonppp(&hdr, data)) == NULLBUF) {
  261.         ppp_log( ppp_p, Nospace );
  262.         ppp_p->OutMemory++;
  263.         return -1;
  264.     }
  265.  
  266.     return (*ifp->raw)(ifp,bp);
  267. }
  268.  
  269.  
  270. /* Encode a raw packet in PPP framing, put on link output queue */
  271. static int
  272. ppp_raw(ifp,bp)
  273. struct iface *ifp;
  274. struct mbuf *bp;
  275. {
  276.     struct ppp_s *ppp_p = ifp->edv;
  277.     struct lcp_s *lcp_p = ppp_p->fsm[Lcp].pdv;
  278.     int full_lcp, full_ac, full_p;
  279.     int16 calc_fcs = HDLC_FCS_START;
  280.     int32 accm = LCP_ACCM_DEFAULT;
  281.     struct ppp_hdr ph;
  282.     int len = PPP_HDR_LEN;
  283.     struct mbuf *vbp;
  284.     register char *cp;
  285.     register int c;
  286.  
  287.     dump(ifp,IF_TRACE_OUT,CL_PPP,bp);
  288.     ppp_p->OutTxOctetCount += len_p(bp) + 2;  /* count FCS bytes */
  289.     ifp->rawsndcnt++;
  290.     ifp->lastsent = secclock();
  291.  
  292.     /* Get the HDLC/PPP header, without actually pulling mbuf up */
  293.     if ( bp == NULLBUF || bp->cnt < PPP_HDR_LEN ) {
  294.         ppp_error( ppp_p, bp, "link header missing" );
  295.         ppp_p->OutError++;
  296.         return -1;
  297.     }
  298.  
  299.     ph.addr = bp->data[0];
  300.     ph.control = bp->data[1];
  301.     ph.protocol = get16(&bp->data[2]);
  302.  
  303.     if ( ( full_lcp = (ph.protocol == PPP_LCP_PROTOCOL) ) == 0
  304.      && (lcp_p->remote.work.negotiate & LCP_N_ACCM) ) {
  305.         accm = lcp_p->remote.work.accm;
  306.     }
  307.  
  308.     if ( ( full_p = ( full_lcp
  309.             ||  !(lcp_p->remote.work.negotiate & LCP_N_PFC)
  310.             ||  ph.protocol >= 0x00ff ) )
  311.         == 0 ) {
  312.         --len;
  313.     }
  314.  
  315.     /* Discard HDLC address and control fields if possible */
  316.     if ( ( full_ac = ( full_lcp
  317.             ||  !(lcp_p->remote.work.negotiate & LCP_N_ACFC) ) )
  318.         == 0 ) {
  319.         len -= 2;
  320.     }
  321.  
  322.     /* Load header with proper values */
  323.     bp->cnt -= PPP_HDR_LEN - len;
  324.     cp = (bp->data += PPP_HDR_LEN - len);
  325.  
  326.     if ( full_ac ) {
  327.         *cp++ = ph.addr;
  328.         *cp++ = ph.control;
  329.     }
  330.     if ( full_p )
  331.         *cp++ = (ph.protocol >> 8);
  332.     *cp++ = (ph.protocol & 0x00ff);
  333.  
  334.     /* Allocate output mbuf that's twice as long as the packet.
  335.      * This is a worst-case guess (consider a packet full of HDLC_FLAGs!)
  336.      */
  337.     if ((vbp = alloc_mbuf((int16)(2*len_p(bp) + HDLC_ENVLEN))) == NULLBUF) {
  338.         ppp_error( ppp_p, bp, Nospace );
  339.         ppp_p->OutMemory++;
  340.         return -1;
  341.     }
  342.     cp = vbp->data;
  343.  
  344.     /* No need to send an opening flag if the previous packet is still
  345.      * being transmitted.
  346.      */
  347. #ifdef UNIX
  348.     if (Asy[ifp->dev].sndq == 0) {
  349. #else
  350.     if ( Asy[ifp->dev].dma.flags == 0 ) {
  351. #endif
  352.         /* Flush out any line garbage */
  353.         *cp++ = HDLC_FLAG;
  354.         ppp_p->OutOpenFlag++;
  355.     }
  356.  
  357.     /* Copy input to output, escaping special characters */
  358.     while ((c = PULLCHAR(&bp)) != -1) {
  359.         /* Fold char value into FCS calculated so far */
  360.         calc_fcs = pppfcs(calc_fcs, c);
  361.  
  362.         if ( ((c < SP_CHAR) && (accm & (1L << c)))
  363.             || (c == HDLC_ESC_ASYNC)
  364.             || (c == HDLC_FLAG)) {
  365.             *cp++ = HDLC_ESC_ASYNC;
  366.             *cp++ = (c ^ HDLC_ESC_COMPL);
  367.         } else {
  368.             *cp++ = c;
  369.         }
  370.     }
  371.  
  372.     /* Final FCS calculation */
  373.     calc_fcs ^= 0xffff;
  374.     c = (calc_fcs & 0x00ff);    /* Least significant byte first */
  375.     if ( ((c < SP_CHAR) && (accm & (1L << c)))
  376.          ||(c == HDLC_ESC_ASYNC)
  377.          ||(c == HDLC_FLAG)) {
  378.         *cp++ = HDLC_ESC_ASYNC;
  379.         *cp++ = (c ^ HDLC_ESC_COMPL);
  380.     } else {
  381.         *cp++ = c;
  382.     }
  383.     c = (calc_fcs >> 8);        /* Most significant byte next */
  384.     if ( ((c < SP_CHAR) && (accm & (1L << c)))
  385.          ||(c == HDLC_ESC_ASYNC)
  386.          ||(c == HDLC_FLAG)) {
  387.         *cp++ = HDLC_ESC_ASYNC;
  388.         *cp++ = (c ^ HDLC_ESC_COMPL);
  389.     } else {
  390.         *cp++ = c;
  391.     }
  392.  
  393.     /* Tie off the packet */
  394.     *cp++ = HDLC_FLAG;
  395.     vbp->cnt = cp - vbp->data;
  396.  
  397.     if (ifp->trace & IF_TRACE_RAW)
  398.         raw_dump(ifp,IF_TRACE_OUT,vbp);
  399.     return asy_send(ifp->dev,vbp);
  400. }
  401.  
  402.  
  403. /****************************************************************************/
  404. /* Packetize PPP input from device */
  405. /* (process started by ppp_init) */
  406. void
  407. ppp_recv(dev,p1,p2)
  408. int dev;
  409. void *p1;
  410. void *p2;
  411. {
  412.     struct iface *ifp = p1;
  413.     struct ppp_s *ppp_p = ifp->edv;
  414.     int32 accm = LCP_ACCM_DEFAULT;
  415.     int16 calc_fcs = HDLC_FCS_START;
  416.     struct mbuf *raw_bp = NULLBUF;
  417.     struct mbuf *head_bp = NULLBUF;
  418.     register struct mbuf *tail_bp = NULLBUF;
  419.     char *cp;            /* next byte in tail mbuf */
  420.     register int mode = FALSE;
  421.     register int c;
  422.  
  423. #ifdef __GNUC__
  424.     cp = 0;            /* silence warning */
  425. #endif
  426.     while ( (c = get_asy(dev)) != -1 ) {
  427. #ifdef PPP_DEBUG_RAW
  428.         if (ifp->trace & IF_TRACE_RAW) {
  429.             if ( raw_bp != NULLBUF
  430.               || (raw_bp = alloc_mbuf( LCP_MRU_HI * 2 )) != NULLBUF ) {
  431.                 *raw_bp->data++ = c;
  432.                 raw_bp->cnt++;
  433.                 if ( raw_bp->cnt != 1 && c == HDLC_FLAG ) {
  434.                     raw_bp->data = (char *)(raw_bp + 1);
  435.                     raw_dump( ifp, IF_TRACE_IN, raw_bp );
  436.                     raw_bp->cnt = 0;
  437.                 }
  438.             }
  439.         }
  440. #endif
  441.         if ( c == HDLC_FLAG ) {
  442.             if ( mode & PPP_ESCAPED ) {
  443.                 ppp_skipped( ppp_p, head_bp,
  444.                     "deliberate cancellation" );
  445.                 ppp_p->InFrame++;
  446.             } else if ( mode & PPP_TOSS ) {
  447.                 free_p( head_bp );
  448.             } else if ( head_bp != NULLBUF ) {
  449.                 if ( calc_fcs != HDLC_FCS_FINAL ) {
  450.                     ppp_skipped( ppp_p, head_bp,
  451.                         "checksum error" );
  452.                     ppp_p->InChecksum++;
  453.                 } else {
  454.                     /* trim off FCS bytes */
  455.                     trim_mbuf(&head_bp, len_p(head_bp)-2);
  456.  
  457.                     net_route( ifp, CL_PPP, head_bp );
  458.                 }
  459.             } else {
  460.                 ppp_p->InOpenFlag++;
  461.             }
  462.  
  463.             /* setup for next buffer */
  464.             mode = FALSE;
  465.             head_bp = tail_bp = NULLBUF;
  466.             calc_fcs = HDLC_FCS_START;
  467.             accm = LCP_ACCM_DEFAULT;
  468.  
  469.             /* Use negotiated values if LCP finished */
  470.             if (ppp_p->fsm[Lcp].state == fsmOPENED) {
  471.                 struct lcp_s *lcp_p = ppp_p->fsm[Lcp].pdv;
  472.  
  473.                 if (lcp_p->local.work.negotiate & LCP_N_ACCM) {
  474.                     accm = lcp_p->local.work.accm;
  475.                 }
  476.             }
  477. #ifdef PPP_DEBUG_RAW
  478.             if (!(ifp->trace & IF_TRACE_RAW)) {
  479.                 if ( raw_bp != NULLBUF ) {
  480.                     free_p( raw_bp );
  481.                     raw_bp = NULLBUF;
  482.                 }
  483.             }
  484. #endif
  485.             continue;
  486.         }
  487.  
  488.         /* We reach here for every byte inside a frame.
  489.          * (The order of the following tests is important.)
  490.          * Discard spurious control characters.
  491.          * Check for escape sequence.
  492.          * (Allow escaped escape.)
  493.          */
  494.         if ( c < SP_CHAR && (accm & (1L << c)) ) {
  495.             continue;
  496.         } else if ( mode & PPP_ESCAPED ) {
  497.             mode &= ~PPP_ESCAPED;
  498.             c ^= HDLC_ESC_COMPL;
  499.         } else if ( c == HDLC_ESC_ASYNC ) {
  500.             mode |= PPP_ESCAPED;
  501.             continue;
  502.         }
  503.  
  504.         /* We reach here with a byte for the buffer.
  505.          * Make sure there is room for it.
  506.          */
  507.         if ( tail_bp == NULLBUF ) {
  508.             if ((tail_bp = alloc_mbuf(PPP_ALLOC)) == NULLBUF) {
  509.                 ppp_skipped( ppp_p, tail_bp, Nospace );
  510.                 ppp_p->InMemory++;
  511.                 mode |= PPP_TOSS;
  512.                 continue;
  513.             }
  514.             head_bp = tail_bp;
  515.             cp = tail_bp->data;
  516.         } else if ( tail_bp->cnt >= tail_bp->size ) {
  517.             /* Current mbuf is full */
  518.             if ( (tail_bp->next = alloc_mbuf(PPP_ALLOC)) == NULLBUF ) {
  519.                 /* No memory, drop the whole packet */
  520.                 ppp_skipped( ppp_p, head_bp, Nospace );
  521.                 ppp_p->InMemory++;
  522.                 head_bp = NULLBUF;
  523.                 mode |= PPP_TOSS;
  524.                 continue;
  525.             }
  526.             tail_bp = tail_bp->next;
  527.             cp = tail_bp->data;
  528.         }
  529.  
  530.         /* Store the byte, increment counts */
  531.         *cp++ = c;
  532.         tail_bp->cnt++;
  533.         calc_fcs = pppfcs(calc_fcs, c);
  534.     }
  535.  
  536.     /* clean up afterward */
  537.     free_p(raw_bp);
  538.     free_p(head_bp);
  539.     ifp->rxproc = NULLPROC;
  540. }
  541.  
  542. #ifdef TURBOC_SWITCH_BUG
  543. #pragma option -G-
  544. #endif
  545.  
  546. /* Process incoming PPP packets */
  547. /* (called from network task) */
  548. void
  549. ppp_proc(ifp,bp)
  550. struct iface *ifp;
  551. struct mbuf *bp;
  552. {
  553.     struct ppp_s *ppp_p;
  554.     struct ipcp_s *ipcp_p;
  555.     struct ppp_hdr ph;
  556.     struct mbuf *hbp;
  557.     int16 negotiated = FALSE;
  558.  
  559.     if ( ifp == NULLIF ) {
  560.         log(-1, "ppp_proc: missing iface" );
  561.         return;
  562.     }
  563.     if ( bp == NULLBUF ) {
  564.         trace_log(ifp, "ppp_proc: missing buffer" );
  565.         return;
  566.     }
  567.  
  568.     ppp_p = ifp->edv;
  569.     ppp_p->InRxOctetCount += len_p(bp) + 2;   /* count FCS bytes */
  570.  
  571.     /* Use negotiated values if LCP finished */
  572.     if (ppp_p->fsm[Lcp].state == fsmOPENED) {
  573.         struct lcp_s *lcp_p = ppp_p->fsm[Lcp].pdv;
  574.  
  575.         negotiated = lcp_p->local.work.negotiate;
  576.     }
  577.  
  578.     /* HDLC address and control fields may be compressed out */
  579.     if ((byte_t)bp->data[0] != HDLC_ALL_ADDR) {
  580.         if (!(negotiated & LCP_N_ACFC)) {
  581.             ppp_skipped( ppp_p, bp, "missing ALL address" );
  582.             ppp_p->InFrame++;
  583.             return;
  584.         }
  585.     } else if ((byte_t)bp->data[1] != HDLC_UI) {
  586.         if (!(negotiated & LCP_N_ACFC)
  587.          || !(negotiated & LCP_N_PFC)) {
  588.             ppp_skipped( ppp_p, bp, "missing UI" );
  589.             ppp_p->InFrame++;
  590.             return;
  591.         }
  592.     } else {
  593.         /* skip address/control fields */
  594.         pull16(&bp);
  595.     }
  596.  
  597.     /* Initialize the expected header */
  598.     ph.addr = HDLC_ALL_ADDR;
  599.     ph.control = HDLC_UI;
  600.     ph.protocol = PULLCHAR(&bp);
  601.  
  602.     /* First byte of PPP protocol field may be compressed out */
  603.     if ( ph.protocol & 0x01 ) {
  604.         if (!(negotiated & LCP_N_PFC)) {
  605.             ppp_skipped( ppp_p, bp, "missing upper protocol byte" );
  606.             ppp_p->InFrame++;
  607.             return;
  608.         }
  609.     } else {
  610.         ph.protocol = (ph.protocol << 8) | PULLCHAR(&bp);
  611.  
  612.         /* Second byte of PPP protocol field must be odd */
  613.         if ( !(ph.protocol & 0x01) ) {
  614.             ppp_skipped( ppp_p, bp, "missing lower protocol byte" );
  615.             ppp_p->InFrame++;
  616.             return;
  617.         }
  618.     }
  619.  
  620.  
  621.     switch(ph.protocol) {
  622.     case PPP_IP_PROTOCOL:    /* Regular IP */
  623.         if ( ppp_p->fsm[IPcp].state != fsmOPENED ) {
  624.             ppp_error( ppp_p, bp, "not open for IP traffic" );
  625.             ppp_p->InError++;
  626.             break;
  627.         }
  628.         ip_route(ifp,bp,0);
  629.         break;
  630.  
  631.     case PPP_COMPR_PROTOCOL:    /* Van Jacobson Compressed TCP/IP */
  632.         if ( ppp_p->fsm[IPcp].state != fsmOPENED ) {
  633.             ppp_skipped( ppp_p, bp, "not open for Compressed TCP/IP traffic" );
  634.             ppp_p->InError++;
  635.             break;
  636.         }
  637.  
  638.         ipcp_p = ppp_p->fsm[IPcp].pdv;
  639.         if (!(ipcp_p->local.work.negotiate & IPCP_N_COMPRESS)) {
  640.             ppp_skipped( ppp_p, bp, "Compressed TCP/IP not enabled" );
  641.             ppp_p->InError++;
  642.             break;
  643.         }
  644.  
  645.         if ( slhc_uncompress(ipcp_p->slhcp, &bp) <= 0 ) {
  646.             ppp_error( ppp_p, bp, "Compressed TCP/IP packet error" );
  647.             ppp_p->InError++;
  648.             break;
  649.         }
  650.         ip_route(ifp,bp,0);
  651.         break;
  652.  
  653.     case PPP_UNCOMP_PROTOCOL:    /* Van Jacobson Uncompressed TCP/IP */
  654.         if ( ppp_p->fsm[IPcp].state != fsmOPENED ) {
  655.             ppp_skipped( ppp_p, bp, "not open for Uncompressed TCP/IP traffic" );
  656.             ppp_p->InError++;
  657.             break;
  658.         }
  659.  
  660.         ipcp_p = ppp_p->fsm[IPcp].pdv;
  661.         if (!(ipcp_p->local.work.negotiate & IPCP_N_COMPRESS)) {
  662.             ppp_skipped( ppp_p, bp, "Uncompressed TCP/IP not enabled" );
  663.             ppp_p->InError++;
  664.             break;
  665.         }
  666.  
  667.         if ( slhc_remember(ipcp_p->slhcp, &bp) <= 0 ) {
  668.             ppp_error( ppp_p, bp, "Uncompressed TCP/IP packet error" );
  669.             ppp_p->InError++;
  670.             break;
  671.         }
  672.         ip_route(ifp,bp,0);
  673.         break;
  674.  
  675.     case PPP_LCP_PROTOCOL:    /* Link Control Protocol */
  676.         ppp_p->InNCP[Lcp]++;
  677.         fsm_proc(&(ppp_p->fsm[Lcp]),bp);
  678.         break;
  679.  
  680.     case PPP_PAP_PROTOCOL:    /* Password Authenticate Protocol */
  681.         if (ppp_p->phase != pppAP
  682.          && ppp_p->phase != pppREADY) {
  683.             ppp_error( ppp_p, bp, "not ready for Authentication" );
  684.             ppp_p->InError++;
  685.             break;
  686.         }
  687.         ppp_p->InNCP[Pap]++;
  688.         pap_proc(&(ppp_p->fsm[Pap]),bp);
  689.         break;
  690.  
  691.     case PPP_IPCP_PROTOCOL:    /* IP Control Protocol */
  692.         if (ppp_p->phase != pppREADY) {
  693.             ppp_error( ppp_p, bp, "not ready for IPCP traffic" );
  694.             ppp_p->InError++;
  695.             break;
  696.         }
  697.         ppp_p->InNCP[IPcp]++;
  698.         fsm_proc(&(ppp_p->fsm[IPcp]),bp);
  699.         break;
  700.  
  701.     default:
  702.         if ( ppp_p->trace )
  703.             trace_log(ppp_p->iface, "%s PPP Unknown packet protocol: %x;",
  704.                 ppp_p->iface->name,
  705.                 ph.protocol);
  706.         ppp_p->InUnknown++;
  707.  
  708.         /* Build Protocol Reject packet:
  709.          * put the header back on ...
  710.          */
  711.         if ((hbp = htonppp(&ph,bp)) == NULLBUF){
  712.             ppp_log( ppp_p, Nospace );
  713.             ppp_p->InMemory++;
  714.             return;
  715.         }
  716.         /* ... then pull off the address and control fields ... */
  717.         pull16(&hbp);
  718.         /* ... and send it as an LCP packet */
  719.         fsm_send( &(ppp_p->fsm[Lcp]), PROT_REJ, 0, hbp );
  720.         break;
  721.     };
  722. }
  723.  
  724. #ifdef TURBOC_SWITCH_BUG
  725. #pragma option -G
  726. #endif
  727.  
  728. /************************************************************************/
  729.  
  730. /* Keep track of changes in I-O status */
  731. /* (called through iface iostatus vector) */
  732. static int
  733. ppp_iostatus( ifp, command, value )
  734. struct iface *ifp;
  735. int command;
  736. int32 value;
  737. {
  738.     struct ppp_s *ppp_p = ifp->edv;
  739.  
  740.     switch ( command ) {
  741.     case PARAM_UP:
  742.         ppp_log( ppp_p, "Physical layer up" );
  743.  
  744.         if ( ppp_p->phase == pppDEAD ) {
  745.             ppp_p->phase = pppLCP;
  746.         }
  747.         fsm_start( &(ppp_p->fsm[Lcp]) );
  748.         return 0;
  749.  
  750.     case PARAM_DOWN:
  751.         ppp_log( ppp_p, "Physical layer down" );
  752.  
  753.         fsm_down( &(ppp_p->fsm[Lcp]) );
  754.         ppp_p->phase = pppDEAD;
  755.         return 0;
  756.     };
  757.     return -1;
  758. }
  759.  
  760.  
  761. static int
  762. ppp_discard(ifp,bp)
  763. struct iface *ifp;
  764. struct mbuf *bp;
  765. {
  766.     struct ppp_s *ppp_p = ifp->edv;
  767.  
  768.     return fsm_send(&(ppp_p->fsm[Lcp]), DISCARD_REQ, 0, bp);
  769. }
  770.  
  771.  
  772. static int
  773. ppp_echo(ifp,bp)
  774. struct iface *ifp;
  775. struct mbuf *bp;
  776. {
  777.     struct ppp_s *ppp_p = ifp->edv;
  778.  
  779.     return fsm_send(&(ppp_p->fsm[Lcp]), ECHO_REQ, 0, bp);
  780. }
  781.  
  782.  
  783. /****************************************************************************/
  784. /* Initialize PPP control structures for a Point-to-Point interface */
  785. int
  786. ppp_init(ifp)
  787. struct iface *ifp;
  788. {
  789.     struct ppp_s *ppp_p;
  790.     char *ifn;
  791.  
  792.     ppp_p = callocw(1,sizeof(struct ppp_s));
  793.  
  794.     ifp->edv = ppp_p;
  795.     ifp->iostatus = ppp_iostatus;
  796.     ifp->raw = ppp_raw;
  797.     ifp->show = ppp_show;
  798.     ifp->echo = ppp_echo;
  799.     ifp->discard = ppp_discard;
  800.  
  801.     ppp_p->iface = ifp;
  802.     ppp_p->phase = pppDEAD;
  803.  
  804.     lcp_init(ppp_p);
  805.     pap_init(ppp_p);
  806.     ipcp_init(ppp_p);
  807.  
  808.     ifp->rxproc = newproc( ifn = if_name( ifp, " receive" ),
  809.             320, ppp_recv, ifp->dev, ifp, NULL, 0);
  810.     free(ifn);
  811.     return 0;
  812. }
  813.  
  814.  
  815. int
  816. ppp_free(ifp)
  817. struct iface *ifp;
  818. {
  819.     struct ppp_s *ppp_p = ifp->edv;
  820.     int fsmi;
  821.  
  822.     alert( ifp->rxproc, -1 );
  823.  
  824.     for ( fsmi = Lcp; fsmi < fsmi_Size; ) {
  825.         fsm_free( &(ppp_p->fsm[fsmi++]) );
  826.     }
  827.     free( ppp_p->peername );
  828.     free( ppp_p );
  829.     return 0;
  830. }
  831.  
  832.